package com.ruoyi.eqmonitor.phase1.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.eqmonitor.domain.OptDeviceMonitorAnalyseData;
import com.ruoyi.eqmonitor.phase1.domain.OptDevicePhase1;
import com.ruoyi.eqmonitor.phase1.domain.OptDevicePhase1MonitorData;
import com.ruoyi.eqmonitor.phase1.mapper.OptDevicePhase1MonitorDataMapper;
import com.ruoyi.eqmonitor.phase1.rabbit.FanoutReceiver;
import com.ruoyi.eqmonitor.phase1.service.IOptDevicePhase1MonitorDataService;
import com.ruoyi.eqmonitor.phase1.service.IOptDevicePhase1Service;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

/**
 * 一期设备监测数据Service业务层处理
 * 
 * @author breeze
 * @date 2023-03-26
 */
@Service
public class OptDevicePhase1MonitorDataServiceImpl extends ServiceImpl<OptDevicePhase1MonitorDataMapper,OptDevicePhase1MonitorData> implements IOptDevicePhase1MonitorDataService {
    
    @Autowired
	private IOptDevicePhase1Service phase1Service;
	
    @Autowired
    private OptDevicePhase1MonitorDataMapper optDevicePhase1MonitorDataMapper;

    @Override
    public OptDevicePhase1MonitorData selectOptDevicePhase1MonitorDataById(Long id){
        return optDevicePhase1MonitorDataMapper.selectOptDevicePhase1MonitorDataById(id);
    }
      
    @Override
    public List<OptDevicePhase1MonitorData> selectOptDevicePhase1MonitorDataList(OptDevicePhase1MonitorData optDevicePhase1MonitorData){
        return optDevicePhase1MonitorDataMapper.selectOptDevicePhase1MonitorDataList(optDevicePhase1MonitorData);
    }

	@Override
	public List<Map<String, Object>> selectOptPhase1DeviceMonitorLastDataList(String deviceCode) {
		return optDevicePhase1MonitorDataMapper.selectOptPhase1DeviceMonitorLastDataList(deviceCode);
	}

	@Override
	public List<Map<String, Object>> deviceMonitorChartAnalyse(String deviceCode, String type) {
        List<Map<String, Object>> resultList = new ArrayList<>();
		String code = "CC0007";
		OptDevicePhase1 device = phase1Service.selectOptDevicePhase1ByDeviceCode(deviceCode);
        if (device == null) {
            return ListUtil.empty();
        }
        
        //获取此设备液位默认阈值,液位阈值默认井深的70%
        Double threshold = null;
        //第一批数据的格式为[井深XXX 量程]
        if(null != device.getRemark() 
        		&& device.getRemark().contains("井深")
        		&& device.getRemark().contains("量程")) {
        	try {
            	threshold = Double.parseDouble(device.getRemark().split("量程")[0].replace(" ", "").replace("井深", "")) * 0.7;
			} catch (Exception e) {
			}
        }
        
        //最近2个月
        Date yesterday = DateUtil.offsetDay(new Date(), -1);
        Date start = DateUtil.beginOfDay(DateUtil.offsetMonth(yesterday, -2));
        List<DateTime> dateTimes = DateUtil.rangeToList(start, yesterday, DateField.DAY_OF_MONTH);
        List<Map<String,Object>> datas = optDevicePhase1MonitorDataMapper.selectDeviceMonitorChartAnalyse(deviceCode,start,yesterday);
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String,Object>> infos = new ArrayList<>();
        //按照日期再进行分组
        Map<String,Map<String,Object>> group = new HashMap<String,Map<String,Object>>();
        for (int i = 0; i < datas.size(); i++) {
        	group.put(datas.get(i).get("monitor_date").toString(), datas.get(i));
		}
        //补全日期区间内的所有数据
        for (DateTime dateTime : dateTimes) {
        	Map<String,Object> info = new HashMap<String,Object>();
        	info.put("abx",device.getAbx());
        	info.put("aby",device.getAby());
        	info.put("dataUnit","m");
        	info.put("monitorName",FanoutReceiver.Phase1MonitorMap.get(code));
        	info.put("code", code);
        	info.put("siteName",device.getSiteName());
        	info.put("deviceCode",device.getDeviceCode());
        	info.put("deviceName",device.getDeviceName());
        	info.put("deviceType",device.getDeviceType());
        	info.put("monitorTime", dateTime.toDateStr());
        	if(group.containsKey(dateTime.toDateStr())) {
        		info.put("codeValue", group.get(dateTime.toDateStr()).get("avg_value"));
        	}else {
        		info.put("codeValue", 0D);
        	}
        	infos.add(info);
        }
        resultMap.put("alarmValue", threshold);
        resultMap.put("infos", infos);
        resultMap.put("code", code);
        resultMap.put("name", FanoutReceiver.Phase1MonitorMap.get(code));
        resultMap.put("unit", "m");
        resultList.add(resultMap);
        return resultList;  
	}
}
